package cm367;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class BookingDetail {
	private Calendar BookDate = Calendar.getInstance();
	private byte BookDatePart;// = 1; // 1=Morning; 2=Afternoon; 3=Evening;
	private String BookVenus; // type depend on Nicki Ho define
	private String VenusDesc; // type depend on Nicki Ho define
	private double VenusPrice; // type depend on Nicki Ho define
	private int BookFacility01 = 0;
	private double BookFacility01Qty = 0;
	private double BookFacility01Price = 0;
	private int BookFacility02 = 0;
	private double BookFacility02Qty = 0;
	private double BookFacility02Price = 0;
	private int BookFacility03 = 0;
	private double BookFacility03Qty = 0;
	private double BookFacility03Price = 0;
	private int BookFacility04 = 0;
	private double BookFacility04Qty = 0;
	private double BookFacility04Price = 0;
	private int BookFacility05 = 0;
	private double BookFacility05Qty = 0;
	private double BookFacility05Price = 0;
	private Calendar TransDateTime = Calendar.getInstance();

	private String dataSeparator = "|";
	private FacilityList facilitylist;

	@ManagedProperty(value = "#{loggedInUser}")
	private LoggedInUser logInUser;

	public LoggedInUser getLogInUser() {
		return logInUser;
	}

	public void setLogInUser(LoggedInUser logInUser) {
		this.logInUser = logInUser;
	}

	public String BookDatePartDesc() {
		String rst = "";
		switch (BookDatePart) {
		case 1:
			rst = "Morning";
			break;
		case 2:
			rst = "Afternoon";
			break;
		case 3:
			rst = "Evening";
			break;
		default:
			rst = "NOT DEFINE!!?";
			break;
		}
		return rst;
	}

	public String BookDateTimeDesc() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		return dateFormat.format(BookDate.getTime()) + "  >>>  "
				+ BookDatePartDesc();
	}

	public BookingDetail() throws IOException {
		super();
		facilitylist = new FacilityList();
	}

	public BookingDetail(String str) throws IOException {
		super();
		facilitylist = new FacilityList();

		str = str.replace('|', '\t');
		String[] tmp = str.split("\t");
		DateFormat formatter;
		Date date = null;
		formatter = new SimpleDateFormat("dd/MM/yyyy");
		try {
			date = (Date) formatter.parse(tmp[1]);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BookDate = Calendar.getInstance();
		BookDate.setTime(date);
		BookDatePart = Byte.parseByte(tmp[2]);
		BookVenus = tmp[3];
		}

	public double BookingTotalPrice() {
		return VenusPrice + BookFacility01Price + BookFacility02Price
				+ BookFacility03Price + BookFacility04Price
				+ BookFacility05Price;

	}

	public String DispFacilityDesc(int num) {
		int idFac;
		String desc = "";
		switch (num) {
		case 1:
			idFac = BookFacility01;
			break;
		case 2:
			idFac = BookFacility02;
			break;
		case 3:
			idFac = BookFacility03;
			break;
		case 4:
			idFac = BookFacility04;
			break;
		case 5:
			idFac = BookFacility05;
			break;
		default:
			idFac = 0;
			break;
		}
		for (int idx = 0; idx < facilitylist.getFacilitiesList().size(); idx++) {
			if (facilitylist.getFacilitiesList().get(idx).getIdFacility() == idFac) {
				desc = facilitylist.getFacilitiesList().get(idx)
						.getFacilityDesc();
			}
		}
		return desc;
	}

	public String CalFacilityPrice(int num) {
		int idFac, idxfac = 0;
		double Qty = 0, Price = 0;

		switch (num) {
		case 1:
			idFac = BookFacility01;
			Qty = BookFacility01Qty;
			break;
		case 2:
			idFac = BookFacility02;
			Qty = BookFacility02Qty;
			break;
		case 3:
			idFac = BookFacility03;
			Qty = BookFacility03Qty;
			break;
		case 4:
			idFac = BookFacility04;
			Qty = BookFacility04Qty;
			break;
		case 5:
			idFac = BookFacility05;
			Qty = BookFacility05Qty;
			break;
		default:
			idFac = 0;
			Qty = -1;
			Price = -1;
			break;
		}
		if (idFac == 0) {
			Price = 0;
		} else {
			for (int idx = 0; idx < facilitylist.getFacilitiesList().size(); idx++) {
				if (facilitylist.getFacilitiesList().get(idx).getIdFacility() == idFac) {
					idxfac = idx;
				}
			}
			try {
				Price = facilitylist.getFacilitiesList().get(idxfac)
						.Formula2Value(Qty);
				Price = Math.round(Price * 100 + 0.5) / 100;
			} catch (NumberFormatException nfe) {
				Price = 0;
			}
		}
		switch (num) {
		case 1:
			BookFacility01Price = Price;
			break;
		case 2:
			BookFacility02Price = Price;
			break;
		case 3:
			BookFacility03Price = Price;
			break;
		case 4:
			BookFacility04Price = Price;
			break;
		case 5:
			BookFacility05Price = Price;
			break;
		}
		return null;
	}

	public BookingDetail(String bookingBy, Calendar bookDate,
			byte bookDatePart, String bookVenus, String venusDesc,
			double venusPrice, int bookFacility01, double bookFacility01Qty,
			double bookFacility01Price, int bookFacility02,
			double bookFacility02Qty, double bookFacility02Price,
			int bookFacility03, double bookFacility03Qty,
			double bookFacility03Price, int bookFacility04,
			double bookFacility04Qty, double bookFacility04Price,
			int bookFacility05, double bookFacility05Qty,
			double bookFacility05Price) {
		super();
		BookDate = bookDate;
		BookDatePart = bookDatePart;
		BookVenus = bookVenus;
		VenusDesc = venusDesc;
		VenusPrice = venusPrice;
		BookFacility01 = bookFacility01;
		BookFacility01Qty = bookFacility01Qty;
		BookFacility01Price = bookFacility01Price;
		BookFacility02 = bookFacility02;
		BookFacility02Qty = bookFacility02Qty;
		BookFacility02Price = bookFacility02Price;
		BookFacility03 = bookFacility03;
		BookFacility03Qty = bookFacility03Qty;
		BookFacility03Price = bookFacility03Price;
		BookFacility04 = bookFacility04;
		BookFacility04Qty = bookFacility04Qty;
		BookFacility04Price = bookFacility04Price;
		BookFacility05 = bookFacility05;
		BookFacility05Qty = bookFacility05Qty;
		BookFacility05Price = bookFacility05Price;
	}

	public String getBookingBy() {
		
		if (logInUser.equals(null)) {
			return "";
		} else {
			return logInUser.getUser().getName();
		}

		 //return "Testing User";
	}

	public Calendar getBookDate() {
		return BookDate;
	}

	public void setBookDate(Calendar bookDate) {
		BookDate = bookDate;
	}

	public byte getBookDatePart() {
		return BookDatePart;
	}

	public void setBookDatePart(byte bookDatePart) {
		BookDatePart = bookDatePart;
	}

	public String getBookVenus() {
		return BookVenus;
	}

	public void setBookVenus(String bookVenus) {
		BookVenus = bookVenus;
	}

	public String getVenusDesc() {
		return VenusDesc;
	}

	public void setVenusDesc(String venusDesc) {
		VenusDesc = venusDesc;
	}

	public double getVenusPrice() {
		return VenusPrice;
	}

	public void setVenusPrice(double venusPrice) {
		VenusPrice = venusPrice;
	}

	public int getBookFacility01() {
		return BookFacility01;
	}

	public void setBookFacility01(int bookFacility01) {
		BookFacility01 = bookFacility01;
	}

	public double getBookFacility01Qty() {
		return BookFacility01Qty;
	}

	public void setBookFacility01Qty(double bookFacility01Qty) {
		BookFacility01Qty = bookFacility01Qty;
	}

	public double getBookFacility01Price() {
		return BookFacility01Price;
	}

	public void setBookFacility01Price(double bookFacility01Price) {
		BookFacility01Price = bookFacility01Price;
	}

	public int getBookFacility02() {
		return BookFacility02;
	}

	public void setBookFacility02(int bookFacility02) {
		BookFacility02 = bookFacility02;
	}

	public double getBookFacility02Qty() {
		return BookFacility02Qty;
	}

	public void setBookFacility02Qty(double bookFacility02Qty) {
		BookFacility02Qty = bookFacility02Qty;
	}

	public double getBookFacility02Price() {
		return BookFacility02Price;
	}

	public void setBookFacility02Price(double bookFacility02Price) {
		BookFacility02Price = bookFacility02Price;
	}

	public int getBookFacility03() {
		return BookFacility03;
	}

	public void setBookFacility03(int bookFacility03) {
		BookFacility03 = bookFacility03;
	}

	public double getBookFacility03Qty() {
		return BookFacility03Qty;
	}

	public void setBookFacility03Qty(double bookFacility03Qty) {
		BookFacility03Qty = bookFacility03Qty;
	}

	public double getBookFacility03Price() {
		return BookFacility03Price;
	}

	public void setBookFacility03Price(double bookFacility03Price) {
		BookFacility03Price = bookFacility03Price;
	}

	public int getBookFacility04() {
		return BookFacility04;
	}

	public void setBookFacility04(int bookFacility04) {
		BookFacility04 = bookFacility04;
	}

	public double getBookFacility04Qty() {
		return BookFacility04Qty;
	}

	public void setBookFacility04Qty(double bookFacility04Qty) {
		BookFacility04Qty = bookFacility04Qty;
	}

	public double getBookFacility04Price() {
		return BookFacility04Price;
	}

	public void setBookFacility04Price(double bookFacility04Price) {
		BookFacility04Price = bookFacility04Price;
	}

	public int getBookFacility05() {
		return BookFacility05;
	}

	public void setBookFacility05(int bookFacility05) {
		BookFacility05 = bookFacility05;
	}

	public double getBookFacility05Qty() {
		return BookFacility05Qty;
	}

	public void setBookFacility05Qty(double bookFacility05Qty) {
		BookFacility05Qty = bookFacility05Qty;
	}

	public double getBookFacility05Price() {
		return BookFacility05Price;
	}

	public void setBookFacility05Price(double bookFacility05Price) {
		BookFacility05Price = bookFacility05Price;
	}

	public Calendar getTransDateTime() {
		return TransDateTime;
	}

	public void setTransDateTime(Calendar transDateTime) {
		TransDateTime = Calendar.getInstance();
	}

	public String SaveBooking() throws IOException {
		String bookingData = getBookingBy().trim() + dataSeparator.trim();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		SimpleDateFormat dateFormat2 = new SimpleDateFormat(
				"dd/MM/yyyy HH:mm:ss");
		bookingData += dateFormat.format(getBookDate().getTime()).trim()
				+ dataSeparator.trim()
				+ Byte.toString(getBookDatePart()).trim()
				+ dataSeparator.trim() + getBookVenus() + dataSeparator.trim()
				+ Double.toString(getVenusPrice()).trim()
				+ dataSeparator.trim()
				+ Integer.toString(getBookFacility01()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility01Qty()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility01Price()).trim()
				+ dataSeparator.trim()
				+ Integer.toString(getBookFacility02()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility02Qty()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility02Price()).trim()
				+ dataSeparator.trim()
				+ Integer.toString(getBookFacility03()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility03Qty()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility03Price()).trim()
				+ dataSeparator.trim()
				+ Integer.toString(getBookFacility04()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility04Qty()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility04Price()).trim()
				+ dataSeparator.trim()
				+ Integer.toString(getBookFacility05()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility05Qty()).trim()
				+ dataSeparator.trim()
				+ Double.toString(getBookFacility05Price()).trim()
				+ dataSeparator.trim()
				+ dateFormat2.format(getTransDateTime().getTime()).trim()
				+ dataSeparator.trim();

		try {
			ReadWriteDataFile dataFile = new ReadWriteDataFile("db_Booking.txt");
			dataFile.AppendDataFile(bookingData);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return "bConfirm";
	}

	public String SetUserNull() {
		logInUser.setUser(null);
		System.out.println("User nulled! ");
		return "bFinish";
	}
}
